package com.sqfw.project.knowledge.controller;


import com.sqfw.project.knowledge.domain.KlSearchHistoryRecord;
import com.sqfw.project.knowledge.service.IKlSearchHistoryRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;

/**
 * 搜索历史记录Controller
 * 
 * @author tangxt
 * @date 2023-06-14
 */
@Api(tags = "知识库检索搜索历史")
@RestController
@RequestMapping("/klSearchHistoryRecord/")
public class KlSearchHistoryRecordController extends BaseController {
    @Autowired
    private IKlSearchHistoryRecordService klSearchHistoryRecordService;


    /**
     * 新增搜索历史记录
     */
    @ApiOperation("新增搜索历史记录")
    @Log(title = "搜索历史记录", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody KlSearchHistoryRecord klSearchHistoryRecord) {
        if (StringUtils.isBlank(klSearchHistoryRecord.getSearchContentJson())) {
            return AjaxResult.error("搜索关键字不能为空");
        }
        // 搜索类型(KNOWLEDGE: 知识检索)
        if (StringUtils.isBlank(klSearchHistoryRecord.getSearchType())) {
            // 默认：KNOWLEDGE: 知识检索
            klSearchHistoryRecord.setSearchType("KNOWLEDGE");
        }
        return toAjax(klSearchHistoryRecordService.insertKlSearchHistoryRecord(klSearchHistoryRecord));
    }

    /**
     * 查询每个用户的搜索历史记录
     * */
    @ApiOperation("查询每个用户的搜索历史记录")
    @PostMapping("queryKlSearchHistoryRecord")
    public AjaxResult queryKlSearchHistoryRecord(@RequestBody KlSearchHistoryRecord klSearchHistoryRecord) {
        // 搜索类型(KNOWLEDGE: 知识检索)
        if (StringUtils.isBlank(klSearchHistoryRecord.getSearchType())) {
            // 默认：KNOWLEDGE: 知识检索
            klSearchHistoryRecord.setSearchType("KNOWLEDGE");
        }
        KlSearchHistoryRecord record = klSearchHistoryRecordService.queryKlSearchHistoryRecord(klSearchHistoryRecord);
        if (record == null) {
            AjaxResult.error("没有搜索记录");
        }
        return AjaxResult.success(record);
    }



    /**
     * 修改搜索历史记录
     */
    @Log(title = "搜索历史记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KlSearchHistoryRecord klSearchHistoryRecord) {
        return toAjax(klSearchHistoryRecordService.updateKlSearchHistoryRecord(klSearchHistoryRecord));
    }


}
